// pages/my/my.js
var navListJson = require('../../data/navListJson');
var util =  require('../../utils/util.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isPic:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      myNavList: navListJson.myNavList
    })
    // this.isCertification();
  },

  //授权登录
  authorizedLoginBtn:function(){
    var that = this
    let isLogin = that.data.userId?true:false
    //未授权
    wx.navigateTo({
      url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ isLogin,
    })
  },

  //导航
  nav:function(e){
    if(app.globalData.userId){
      var page = e.currentTarget.dataset.page;
      switch (page) {
        case 'QrCode':
          // wx.navigateTo({
          //   url: '/subPages/mine/QrCode/QrCode'
          // })
          util.showToast('正在开发...','loading')
          break;
        case 'myDevice':
          wx.navigateTo({
            url: '/subPages/mine/myDevice/myDevice'
          })
          break;
        case 'orderList':
            wx.navigateTo({
                url: '/subPages/mine/orderList/orderList'
            })
            break;
        case 'integral':
            wx.navigateTo({
                url: '/subPages/mine/integral/integral'
            })
            break;
        case 'module':
          wx.navigateTo({
            url: '/subPages/mine/module/module'
          })
          break;
        case 'health':
          wx.navigateTo({
            url: '/subPages/mine/health/health'
          })
          break;
        case 'share':
          wx.navigateTo({
            url: '/subPages/mine/share/share'
          })
          break;
        case 'publicWelfare':
          wx.navigateTo({
            url: '/subPages/mine/publicWelfare/publicWelfare'
          })
          break;
        default:
          util.showToast('正在开发...','loading')
          break;
      }
    }else{
      util.showModelAll('提示','该功能需要授权登录后使用，请进行授权登录',function(){
        if (app.globalData.userInfo) {
          //已授权
          wx.redirectTo({
            url: '/pages/login/login'
          })
        }else{
          // 在没有 open-type=getUserInfo 版本的兼容处理
          if(app.globalData.isAuthorized){
            app.userInfoReadyCallback = res => {
              if (res) {
                //已授权
                wx.redirectTo({
                  url: '/pages/login/login'
                })
              }else{
                //未授权
                wx.navigateTo({
                  url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ false,
                })
              }
            }
          }else{
            wx.navigateTo({
              url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ false,
            })
          }
        }
      })
    }
  },

  //签到
  signIn:function(){
    if(app.globalData.userId){
      wx.navigateTo({
        url: '/subPages/mine/signIn/signIn'
      })
    }else{
      util.showModelAll('提示','该功能需要授权登录后使用，请进行授权登录',function(){
        if (app.globalData.userInfo) {
          //已授权
          wx.redirectTo({
            url: '/pages/login/login'
          })
        }else{
          // 在没有 open-type=getUserInfo 版本的兼容处理
          if(app.globalData.isAuthorized){
            app.userInfoReadyCallback = res => {
              if (res) {
                //已授权
                wx.redirectTo({
                  url: '/pages/login/login'
                })
              }else{
                //未授权
                wx.navigateTo({
                  url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ false,
                })
              }
            }
          }else{
            wx.navigateTo({
              url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ false,
            })
          }
        }
      })
    }
  },

  //个人资料
  profile:function(){
    if(app.globalData.userId){
      wx.navigateTo({
        url: '/subPages/mine/profile/profile'
      })
    }else{
      util.showModelAll('提示','该功能需要授权登录后使用，请进行授权登录',function(){
        if (app.globalData.userInfo) {
          //已授权
          wx.redirectTo({
            url: '/pages/login/login'
          })
        }else{
          // 在没有 open-type=getUserInfo 版本的兼容处理
          if(app.globalData.isAuthorized){
            app.userInfoReadyCallback = res => {
              if (res) {
                //已授权
                wx.redirectTo({
                  url: '/pages/login/login'
                })
              }else{
                //未授权
                wx.navigateTo({
                  url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ false,
                })
              }
            }
          }else{
            wx.navigateTo({
              url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ false,
            })
          }
        }
      })
    }
  },

  //等级福利
  welfare:function(){
    util.showToast('正在开发...','loading')
  },

  //是否认证
  isCertification: function(){
    var that = this
    wx.request({
      url: app.globalData.apiUrl + '/renz',
      data: {
        userId: app.globalData.userId,
      },
      method: 'get',
      header: {
        'content-type': 'application/json' //默认值
      },
      success: function (res) {
        // console.log(res)
        if(res.data.info){
          that.setData({
            userPic: res.data.info.userPic,
            nickName: res.data.info.nickName,
            isPic:true
          })
        }
        if(res.data.code == '200'){
          let index = 0
          for(var i in res.data.data){
            if(res.data.data[i].status == 2)
            {
              index++
            }
          }
          if(index > 0){
            that.navigatorIsShow();
          }
          that.setData({
            isCertification: 2,
            stateList: JSON.stringify(res.data.data)
          })
        }
        else if(res.data.code == '300')
        {
          that.setData({
            isCertification: 1
          })
        }
      },
      fail: function (res) {
        console.log("失败");
      }
    })
  },

  //我的团队是否显示
  navigatorIsShow:function(){
    var that = this;
    var arr = that.data.myOptionsList;
    for(var i in arr)
    {
      if(arr[i].text == "我的团队")
      {
        arr[i].isShow = true;
        break;
      }
    }
    that.setData({
      myOptionsList: arr
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow:function(){
    // this.isCertification();
    var that = this
    if (app.globalData.userInfo) {
      that.setData({
        userInfo: app.globalData.userInfo
      })
    }else{
       // 在没有 open-type=getUserInfo 版本的兼容处理
       app.userInfoReadyCallback = res => {
        if (res) {
          that.setData({
            userInfo: res.userInfo
          });
        }
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: 'PAPN健康管理',
      path: 'pages/index/index',
      success: function(res) {
        // res.shareTickets
      },
      fail: function(res) {
          console.log(res)
      }
    }
  }
})
